﻿@page "/recurringjobs/{action}"
@layout SimpleIdServer.IdServer.Website.Shared.MainLayout
@attribute [Authorize("Authenticated")]
@using Fluxor.Blazor.Web.Components
@using Hangfire.Storage.Monitoring
@using SimpleIdServer.IdServer.Api.RecurringJobs
@using SimpleIdServer.IdServer.Website.Resources
@using SimpleIdServer.IdServer.Website.Shared.Common
@using SimpleIdServer.IdServer.Website.Shared.Dialogs
@using SimpleIdServer.IdServer.Website.Stores.RecurringJobStore
@inject ContextMenuService contextMenuService
@inject DialogService dialogService
@inject NotificationService notificationService
@inject NavigationManager navigationManager
@inject IDispatcher dispatcher
@inject IUrlHelper urlHelper

<SidAuthorizeView Roles=@("/recurringjobs/manage,/recurringjobs/view")>
    <Authorized>
        <RadzenTabs Change=@OnChange @bind-SelectedIndex="selectedIndex">
            <Tabs>
                <!-- Jobs -->
                <RadzenTabsItem Text="@Global.Jobs">
                    <RecurringJobsComponent></RecurringJobsComponent>
                </RadzenTabsItem>
                <!-- Failed jobs -->
                <RadzenTabsItem Text="@Global.FailedJobs">
                    <FailedJobsComponent></FailedJobsComponent>
                </RadzenTabsItem>
            </Tabs>
        </RadzenTabs>
    </Authorized>
    <NotAuthorized>
        <p>@Global.NotAuthorized</p>
    </NotAuthorized>
</SidAuthorizeView>

@code {
    Dictionary<string, int> mappingActionNameToIndex = new Dictionary<string, int>
    {
        { "list", 0 },
        { "failedjobs", 1 }
    };

    [Parameter]
    public string action 
    { 
        get; set; 
    }

    int selectedIndex = 0;

    protected override void OnParametersSet()
    {
        base.OnParametersSet();
        if (!string.IsNullOrWhiteSpace(action) && mappingActionNameToIndex.ContainsKey(action))
        {
            selectedIndex = mappingActionNameToIndex[action];
        }
    }

    void OnChange(int index)
    {
        var rec = mappingActionNameToIndex.Single(kvp => kvp.Value == index);
        navigationManager.NavigateTo(urlHelper.GetUrl($"/recurringjobs/{rec.Key}"));
    }
}